package com.xfcycle.zebrabikegp.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.xfcycle.zebrabikegp.R;
import com.xfcycle.zebrabikegp.assist.BlueToothAssist;
import com.xfcycle.zebrabikegp.constant.ActivityChangeConstant;
import com.xfcycle.zebrabikegp.constant.ToastConstant;
import com.xfcycle.zebrabikegp.constant.WebConstant;
import com.xfcycle.zebrabikegp.util.HandleDataByBLEUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * 改变自行车的名称
 * <p>
 * Created by xFcycle on 2017/5/19.
 */

public class BikeNikenameChangeActivity extends RootActivity {
    private static final String TAG = "ChangeBikeNikeName";

    private Bundle bundle;

    private ImageView iv_bike_name_back;
    private Button bt_change_bike_nickname_send;

    private EditText et_change_bike_nickname;

    private String change_bike_nickname;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_bike_name);

        bundle = this.getIntent().getExtras();

        initView();
        initViewListener();
    }

    /***********************************************************************************************/
    /**                                 初始化控件对象 监听 控件内容方法                             **/
    /***********************************************************************************************/

    private void initView() {
        iv_bike_name_back = (ImageView) findViewById(R.id.iv_bike_name_back);

        bt_change_bike_nickname_send = (Button) findViewById(R.id.bt_change_bike_nickname_send);

        et_change_bike_nickname = (EditText) findViewById(R.id.et_change_bike_nickname);
    }

    private void initViewListener() {
        //回退事件监听
        iv_bike_name_back.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                closeActivity();
            }
        });

        //发送给服务器，改变自行车名称
        bt_change_bike_nickname_send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null == et_change_bike_nickname.getText() || "".equals(et_change_bike_nickname.getText().toString())) {
                    Toast.makeText(BikeNikenameChangeActivity.this.getApplicationContext(), "Context can't be empty..", Toast.LENGTH_SHORT).show();
                    return;
                }

                String cid = bundle.getString("cid");

                change_bike_nickname = et_change_bike_nickname.getText().toString().replace("%","%25").replace("+","%2B").replace(" ","%20");

                Map<String, String> params = new HashMap<>();
                params.put(WebConstant.SEND_BIKE_INFO_PATH_PARAM_NICKNAME, change_bike_nickname);
                new GetJsonFromWebJob(WebConstant.SEND_BIKE_INFO_PATH + cid, WebConstant.PUT, params, REQUEST_SIGN_CHANGE_BIKE_NICKNAME_BY_CID, BikeNikenameChangeActivity.this).start();
            }
        });
    }

    /***********************************************************************************************/
    /**                             处理从不同途径获得的数据                                         **/
    /***********************************************************************************************/

    /**
     * 处理从网络获得的数据
     *
     * @param webMsg
     */
    @Override
    public void dealDataFromWeb(Message webMsg) {
        super.dealDataFromWeb(webMsg);

        String webResult = webMsg.obj.toString();
        int webSign = webMsg.what;

        Log.e(TAG, "dealDataFromWeb: -----------" + webResult);

        //改车名
        if (REQUEST_SIGN_CHANGE_BIKE_NICKNAME_BY_CID == webSign) {
            if (!"".equals(webResult)) {
                Log.e(TAG, "dealDataFromWeb: 改车名失败" + webResult);
                Toast.makeText(this.getApplicationContext(), ToastConstant.CHANGE_BIKENAME_FAILURE, Toast.LENGTH_SHORT).show();
                return;
            }

            Bundle backBundle = new Bundle();
            backBundle.putString(ActivityChangeConstant.ACTIVITY_CHANGE_BIKE_NICKNAME_KEY, change_bike_nickname);
            setResult(RESULT_OK, new Intent().putExtras(backBundle));
            finish();
        }

        //判断在其他设备登陆
        if (webMsg.obj.toString().contains("you are signed in at another location") && REQUEST_SIGN_LOGOUT != webMsg.what) {
            //断开连接
            HandleDataByBLEUtil.disConnectDevice();
            //改变蓝牙辅助判断类的值
            BlueToothAssist.isConnect = false;
            BlueToothAssist.currentConnectBikeId = null;

            try {
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle(ToastConstant.DIALOG_WARNING);
                builder.setMessage(ToastConstant.OTHER_DEVICE_LOGIN);

                builder.setPositiveButton(ToastConstant.DIALOG_I_KNOW, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        //回到登录界面
                        activityChange(LOGIN_ACTIVITY, null, null);
                        HandleDataByBLEUtil.disConnectDevice();
                    }

                });
                //展示提示框
                builder.create().show();
            }catch (Exception e){
                Toast.makeText(this.getApplicationContext(), ToastConstant.OTHER_DEVICE_LOGIN, Toast.LENGTH_LONG).show();
                activityChange(LOGIN_ACTIVITY, null, null);
                HandleDataByBLEUtil.disConnectDevice();
            }
            return;
        }

    }
}
